(function () {
    window.EventListener = cc.Class({
        properties: {
            className: "EventListener",
            register: null,
        },
        ctor: function () {
            this.register = {};
        },
        /**
         *  target: 监听器绑定的对象（类名字符串） 例:"EventListener"
         *  name: 监听消息的ID（string/number） 例:2001 / Refresh_card 
         *  method: 监听的方法
         */
        addListener: function (target, name, method) {
            if (!this.register.hasOwnProperty(target)) {
                this.register[target] = {};
            }
            if (!this.register[target].hasOwnProperty(name)) {
                this.register[target][name] = [];
            }
            this.register[target][name].push(method);
        },
        removeListener: function (target) {
            console.log("off handler target = " + target);
            if (this.register.hasOwnProperty(target)) {
                delete this.register[target];
            }
        },
        postMessage: function (obj) {
            // console.log("post: " + JSON.stringify(obj));
            var name = obj.code;
            var data = obj.data;
            for (var id in this.register) {
                var obj = this.register[id];
                if (obj.hasOwnProperty(name)) {
                    var handlerList = obj[name];
                    for (var j = 0; j < handlerList.length; j++) {
                        (function (j) {
                            handlerList[j](data);
                        })(j);
                    }
                }
            }
        }
    });
})();